package com.zcz.item.controller;

import com.zcz.client.UMengClient;
import com.zcz.common.pojo.PageResult;
import com.zcz.item.pojo.Brand;
import com.zcz.item.service.BrandService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 王卫帅
 */
@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private UMengClient uMengClient;
    @Autowired
    private RestTemplate restTemplate;
    private String umengservice = "UMENGPUSH-SERVICE";

    @GetMapping("/3")
    @ResponseBody
    public  ResponseEntity< String> b(HttpServletRequest request){
        System.err.println("++++++++++++++++");
        String ips = request.getRemoteAddr();
        String a = "aaaa";
        int[] arr = {1,2,3};

        try {
            int i=arr[5];
            System.err.println(i);
        }catch (Exception e){
            return ResponseEntity.ok(a);
        }

        return ResponseEntity.ok(a);
    }
    @GetMapping("/1")
    public  ResponseEntity< String> bb(HttpServletRequest request){
        System.err.println("++++++++++++++++");
        String ips = request.getRemoteAddr();
        System.err.println(ips);
        ResponseEntity<String> stringResponseEntity = restTemplate.getForEntity("http://" + umengservice + "/1/1", String.class);
        String a = "aaaa";
        // pushMsgService.callProcedure();

        return ResponseEntity.ok(a);
    }

    @GetMapping("/2")
    @ResponseBody
    public  ResponseEntity<String> ss(){
    //public String ss() throws JSONException {

        //ResponseEntity<String> voidResponseEntity = uMengClient.sendBroadcast("1");
       // System.err.println("+++++++++++++++++********"+voidResponseEntity);
        String bb = "nihao";//template
//        JSONObject json = new JSONObject();
//        json.put("template","1");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("template","1");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://" + umengservice + "/uMengPush/sendBroadcast", request, String.class);
        System.err.println("+++++++++++++++++++++");
        // System.err.println(forObject);
        //System.err.println(forObject+"***************");
        //ResponseEntity<String> r = bb;
        //return ResponseEntity.ok(forObject);
        return stringResponseEntity;
    }

    /**
     * 根据查询条件分页查询
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryb(@RequestParam(value = "key",required = false) String key,
                                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                    @RequestParam(value = "rows",defaultValue = "5") Integer rows,
                                                    @RequestParam(value = "sortBy",required = false) String sortBy,
                                                    @RequestParam(value = "desc",required = false) boolean desc){
        PageResult<Brand> result = this.brandService.queryBrandsByPage(key,page,rows,sortBy,desc);
        if (CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
    @PostMapping("in")
    public ResponseEntity <Void> saveBrand(Brand brand,@RequestParam("cids")List<Long> cids){
        this.brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
